import requests
import json


class Webrequests(object):

    def get(self, url, para, headers):
        try:
            r = requests.get(url, params=para, headers=headers)
            print("获取返回的状态码", r.status_code)
        except BaseException as e:
            print("请求失败！", str(e))
        finally:
            if r.status_code == 200:
                return r.json()
            else:
                return None

    def post(self, url, para, headers):
        try:
            r = requests.post(url, data=para, headers=headers)
            print("获取返回的状态码", r.status_code)
        except BaseException as e:
            print("请求失败！", str(e))
        finally:
            if r.status_code == 200:
                return r.json()
            else:
                return None

    def post_json(self, url, para, headers):
        try:
            data = para
            data = json.dumps(data)  # python数据类型转化为json数据类型
            r = requests.post(url, data=data, headers=headers)
            print("获取返回的状态码", r.status_code)
        except BaseException as e:
            print("请求失败！", str(e))
        finally:
            if r.status_code == 200:
                return r.json()
            else:
                return None
